<template>
  <div class="app-container">

    <CommonTable ref="commonTable" :qo="qo" :baba="this" :need-load-dict-type="['system_user_state']" :async-load-entity-file-count="true">
      <!-- 查询参数 -->
      <template slot="filterItems">
        <el-form-item label="用户名" prop="keyword">
          <el-input v-model.trim="qo.keyword" clearable />
        </el-form-item>
      </template>
      <!-- 列表头 -->
      <template slot="column">
        <el-table-column label="用户名" resizable prop="username" />
        <el-table-column label="昵称" prop="nickname" />
        <el-table-column label="手机" prop="phone" />
        <el-table-column label="邮箱" prop="mail" />
        <el-table-column label="注册日期" prop="dateCreated" />
        <el-table-column label="文件" prop="fileCount">
          <template slot-scope="{ row }">
            <el-link v-show="row.fileCount > 0" type="primary" @click="showFileWindow(row)">附件({{ row.fileCount }})</el-link>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="{row}">
            <el-tag :type="row.state === 1 ? 'success' : 'danger'" effect="light">{{ 'system_user_state'|transferDictValue(row.state) }}</el-tag>
          </template>
        </el-table-column>
      </template>
    </CommonTable>

    <Form ref="form" />

    <!--附件窗口-->
    <SystemFileWindow ref="sfw" :zip="false" />
  </div>
</template>

<script>
import { list, resetPassword, enableAccount, disableAccount } from '@/api/system/user'
import SystemFileWindow from '@/components/SystemFileWindow'
import Form from './form'
import CommonTable from '@/components/CommonTable'
export default {
  name: 'User',
  components: { Form, SystemFileWindow, CommonTable },
  data() {
    return {
      qo: { keyword: '' }
    }
  },
  created() {
  },
  methods: {
    getData(qoPro) {
      return list(qoPro)
    },
    create() {
      this.$refs.form.createOpen()
    },
    update(row) {
      this.$refs.form.updateOpen(row.id)
    },
    resetPassword(row) {
      this.$confirm('确认重置用户 ' + row.username + ' 密码', '提示', {
        type: 'warning'
      }).then(() => {
        const ld = this.$loading()
        setTimeout(() => {
          resetPassword(row.id).then(res => {
            this.$message.success('重置成功')
            ld.close()
          }).catch(() => {
            ld.close()
          })
        }, 400)
      }).catch(() => {})
    },
    enableAccount(row) {
      this.$confirm('确认启用用户 ' + row.username, '提示', {
        type: 'warning'
      }).then(() => {
        const ld = this.$loading()
        setTimeout(() => {
          enableAccount(row.id).then(res => {
            this.$message.success('用户 ' + row.username + ' 已启用')
            ld.close()
            this.getData()
          }).catch(() => { ld.close() })
        }, 400)
      }).catch(() => {})
    },
    disableAccount(row) {
      this.$confirm('确认禁用用户 ' + row.username, '提示', {
        type: 'warning'
      }).then(() => {
        const ld = this.$loading()
        setTimeout(() => {
          disableAccount(row.id).then(res => {
            this.$message.success('用户 ' + row.username + ' 已禁用')
            ld.close()
            this.getData()
          }).catch(() => { ld.close() })
        }, 400)
      }).catch(() => {})
    },
    showFileWindow(row) {
      this.$refs.sfw.showFileList(row.id, '用户附件')
    }
  }
}
</script>
